package com.maomao.handler;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.maomao.config.TenantContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author mao
 * @date 2025-06-10- 14:45
 * @descript
 */
public class TenantInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(StringUtils.isBlank(TenantContext.getCurrentTenant())){
            TenantContext.setCurrentTenant(request.getHeader("tenant_id"));
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        TenantContext.clear();
    }
}
